﻿/*****************************************************************************
* File			: src.Key
* Purpose		: Contains Declaration and Implementation for Key class
* Creation date : April 24, 2009
* Author		: Sami TOUIL
******************************************************************************/
package src {
	
	import flash.display.Stage;
    import flash.events.Event;
    import flash.events.KeyboardEvent;

	/**************************************
	* Class		: Game
	* Purpose	: class defined to behave like the old AS2 Key class
	* Parent	: MovieClip
	**************************************/
    public class Key 
	{
		// Class variables
        private static var bInitialized:Boolean = false;	// Indicates wether the class have already been initialized or not
        private static var keysDown:Object = new Object();  // stores key codes of all keys pressed

		/***********************************************************
		* Method		: initialize
		* Purpose		: Must be called once before using the class
		* Parameters	: stage:Stage : current stage
		* return		: void
		***********************************************************/
        public static function initialize(stage:Stage) 
		{
            if (!bInitialized) 
			{
                // assign listeners for key presses and deactivation of the player
                stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
                stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
                stage.addEventListener(Event.DEACTIVATE, clearKeys);
                
                // set initialization to true
                bInitialized = true;
            }
        }
        
		/***********************************************************
		* Method		: isDown
		* Purpose		: Return true if the key KeyCode is being pressed
		* Parameters	: keyCode:uint : the key we want to test
		* return		: Boolean 
		***********************************************************/
        public static function isDown(keyCode:uint):Boolean 
		{
            return Boolean(keyCode in keysDown);
        }
        
		/***********************************************************
		* Method		: keyPressed
		* Purpose		: 
		* Parameters	: event:KeyboardEvent
		* return		: void 
		***********************************************************/
        private static function keyPressed(event:KeyboardEvent):void 
		{
            keysDown[event.keyCode] = true;
        }
        
		/***********************************************************
		* Method		: keyReleased
		* Purpose		: 
		* Parameters	: event:KeyboardEvent
		* return		: void 
		***********************************************************/
        private static function keyReleased(event:KeyboardEvent):void 
		{
            if (event.keyCode in keysDown) 
                delete keysDown[event.keyCode];
        }
        
		/***********************************************************
		* Method		: clearKeys
		* Purpose		: 
		* Parameters	: event:Event
		* return		: void 
		***********************************************************/
        private static function clearKeys(event:Event):void 
		{
            keysDown = new Object();
        }
    }
}